import { defineStore } from "pinia";

interface countStateType {
  num: number;
}

// defineStore方法返回一个函数，我们可以把这个函数当作是自定义hooks
const useCountStore = defineStore("count", {
  // state建议书写为箭头函数，方便书写ts类型
  state: (): countStateType => {
    return {
      num: 1,
    };
  },
  actions: {
    increment() {
      this.num++;
    },
    incrementN(n: number) {
      this.num += n;
    },
    incrementWaitN(n: number) {
      setTimeout(() => {
        this.num += n;
      }, 2000);
    },
  },
  getters: {
    doubleNum(state: countStateType) {
      return state.num * 2;
    },
  },
});

export default useCountStore;
